#!/usr/bin/python3
# _*_ coding:utf-8 _*_
# __author__ = '__Csm__'

from functools import wraps
from django.http.response import HttpResponseBadRequest
from django.views import View
from django.core.exceptions import PermissionDenied


def ajax_required(f):
    """验证是否为AJAX请求"""
    @wraps(f)
    def wrap(request, *args, **kwargs):
        # request.is_ajax()方法判断是否为AJAX请求
        if not request.is_ajax():
            return HttpResponseBadRequest("不少AJAX请求")
        return f(request, *args, *kwargs)
    return wrap


class AuthorRequireMixin(View):
    """
    验证是否为原作者，用于状态删除，文章编辑；
    """
    def dispatch(self, request, *args, **kwargs):
        # 状态和文章有user属性
        if self.get_object().user.username != self.request.user.username:
            raise PermissionDenied
        return super().dispatch(request, *args, **kwargs)
